using System;
using System.Collections.Generic;
using System.Text;
using Jbe.TestSuite.Infrastructure.Interface.Services;
using System.Windows.Forms;
using System.Collections;
using Microsoft.Practices.CompositeUI.Utility;

namespace Jbe.TestSuite.Infrastructure.Module.Services
{
    internal class MenuItemCollection : IList<IToolBarItem>
    {
        private List<IToolBarItem> list;
        private ToolStripItemCollection innerCollection;

        public MenuItemCollection(ToolStripItemCollection innerCollection)
        {
            list = new List<IToolBarItem>();
            this.innerCollection = innerCollection;
        }

        public IToolBarItem this[int index]
        {
            get { return list[index]; }
            set { list[index] = value; }
        }

        public int Count
        {
            get { return list.Count; }
        }

        public void Add(IToolBarItem item)
        {
            CheckItemType(item);
            list.Add(item);
            innerCollection.Add((WinFormsMenuItem)item);
        }

        public void Insert(int index, IToolBarItem item)
        {
            CheckItemType(item);
            list.Insert(index, item);
            innerCollection.Insert(index, ((WinFormsMenuItem)item));
        }

        public bool Contains(IToolBarItem item)
        {
            return list.Contains(item);
        }

        public int IndexOf(IToolBarItem item)
        {
            return list.IndexOf(item);
        }

        public bool Remove(IToolBarItem item)
        {
            innerCollection.Remove((WinFormsMenuItem)item);
            return list.Remove(item);
        }

        public void RemoveAt(int index)
        {
            innerCollection.RemoveAt(index);
            list.RemoveAt(index);
        }

        public void Clear()
        {
            innerCollection.Clear();
            list.Clear();
        }

        public void CopyTo(IToolBarItem[] array, int arrayIndex)
        {
            list.CopyTo(array, arrayIndex);
        }

        bool ICollection<IToolBarItem>.IsReadOnly
        {
            get { return ((ICollection<IToolBarItem>)list).IsReadOnly; }
        }

        public List<IToolBarItem>.Enumerator GetEnumerator()
        {
            return list.GetEnumerator();
        }

        IEnumerator<IToolBarItem> IEnumerable<IToolBarItem>.GetEnumerator()
        {
            return GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        private static void CheckItemType(IToolBarItem item)
        {
            Guard.ArgumentNotNull(item, "item");
            Guard.TypeIsAssignableFromType(typeof(WinFormsMenuItem), item.GetType(), "item");
        }
    }
}
